#include <vector>
#include <unordered_map>

using namespace std;

class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        vector<int> stack;
        unordered_map<int, int> res;
        for (auto& num : nums2) {
            while (stack.size() and num > *stack.rbegin()) {
                res[*stack.rbegin()] = num;
                stack.pop_back();
            }
            stack.push_back(num);
        }
        for (auto& num : nums1) {
            if (res.find(num) != res.end()) {
                num = res[num];
            }
            else {
                num = -1;
            }
        }
        return nums1;
    }
};